<template>
  <el-row type="flex" class="chat-wrap">
    <el-row class="chat-messages">
      <el-scrollbar ref="messagesScrollbar">
        <div
          class="message"
          :class="item.id == roomStore.state.me.userId ? 'sent' : 'receive'"
          v-for="(item, index) in messages"
          :key="index"
        >
          <el-row
            type="flex"
            align="middle"
            :style="{
              flexDirection: item.id == roomStore.state.me.userId ? 'row' : 'row-reverse'
            }"
          >
            <div class="time">
              {{ item.ts | parseTime('{h}:{i}:{s}') }}
            </div>
            <div class="nickname">
              {{ item.id == roomStore.state.me.userId ? '我' : item.account }}
            </div>
            <el-avatar :size="24" :src="item.userAvatar" icon="el-icon-s-custom"></el-avatar>
          </el-row>
          <div class="content">{{ item.text }}</div>
        </div>
      </el-scrollbar>
    </el-row>
    <div class="message-panel">
      <el-row type="flex" align="middle">
        <el-checkbox
          class="message-banned"
          v-model="muteChat"
          @change="handleMute"
          v-if="muteControl"
          >禁言</el-checkbox
        >
        <el-row
          v-if="isTeacherCourseHide"
          type="flex"
          align="middle"
          justify="center"
          class="message-icon"
          @click.native="course"
        >
          <i class="edu-icon-store"></i>
        </el-row>
      </el-row>
      <div class="message-speak">
        <el-input
          type="textarea"
          :disabled="disableChat"
          v-model="value"
          size="mini"
          :autosize="{ minRows: 4 }"
          :placeholder="disableChat ? '禁言中' : '说点什么'"
          class="message-input"
        ></el-input>
        <el-row type="flex" justify="end">
          <el-button :disabled="disableChat" type="primary" size="mini" @click="sendMessage"
            >发送</el-button
          >
        </el-row>
      </div>
    </div>
  </el-row>
</template>

<script>
import { roomStore } from '../utils/room'
export default {
  data() {
    return {
      value: '',
      roomStore: roomStore,
      muteChat: roomStore.state.course.muteChat
    }
  },
  computed: {
    messages() {
      return this.roomStore.state.messages
    },
    disableChat() {
      if (
        +this.roomStore.state.me.role === 2 &&
        (this.roomStore.state.course.muteChat || !this.roomStore.state.me.chat)
      )
        return true
      return false
    },
    muteControl() {
      return this.roomStore.state.me.role === 1
    },
    accountInfo() {
      return this.$store.getters['user/accountInfo']
    },
    // 教师隐藏带货
    isTeacherCourseHide() {
      if (this.roomStore.state.me.role === 1) {
        return false
      }
      return true
    }
  },
  watch: {
    // 监听消息列表发生变化，滚动条滑动到底部
    messages: {
      handler(now, old) {
        let wrap = this.$refs['messagesScrollbar'].$refs['wrap']
        this.$nextTick(() => {
          wrap.scrollTop = wrap.scrollHeight
        })
      },
      deep: true
    }
  },
  methods: {
    async sendMessage() {
      if (this.value == '') return
      await this.roomStore.sendChatMessage({
        message: this.value,
        userAvatar: this.accountInfo.avatar_url
      })
      const me = this.roomStore.state.me
      const message = {
        account: me.account,
        id: me.userId,
        text: this.value,
        ts: +Date.now(),
        userAvatar: this.accountInfo.avatar_url
      }
      this.roomStore.updateChannelMessage(message)
      this.value = ''
    },
    // 禁言
    async handleMute(value) {
      await this.roomStore.updateCourse({
        muteChat: this.muteChat ? 1 : 0
      })
    },
    course() {
      this.$store.commit('live/CourseDialogVisible', true)
    }
  }
}
</script>

<style lang="scss" scoped>
.chat-wrap {
  flex: 1;
  flex-direction: column;
}
.chat-messages {
  min-width: 280px;
  max-height: calc(100vh - 386px);
  flex: 1;
  flex-direction: column;
  border-bottom: 1px solid #606266;
  ::v-deep .el-scrollbar {
    height: 100%;
    .el-scrollbar__wrap {
      overflow-x: auto;
    }
  }
  .message {
    margin: 10px 0;
    padding: 0 10px;
    flex-shrink: 0;
    &.sent {
      display: flex;
      flex-direction: column;
      align-items: flex-end;
    }
    &.receive {
      display: flex;
      flex-direction: column;
      align-items: flex-start;
    }
    .time {
      font-size: 13px;
      font-weight: 400;
      color: #b7bac1;
    }
    .nickname {
      color: #5191ff;
      font-size: 13px;
      margin: 0 8px;
    }
    .content {
      margin-top: 10px;
      max-width: 260px;
      padding: 8px;
      border-radius: 4px;
      font-size: 13px;
      color: #fff;
      background: rgba(216, 216, 216, 0.1);
      border-radius: 2px;
    }
    .el-avatar--icon {
      font-size: 12px;
    }
  }
}
.message-panel {
  padding: 10px;
  position: relative;
  .message-icon {
    width: 24px;
    height: 24px;
    color: #ff8a52;
    font-size: 14px;
    background-color: #ffffff;
    border-radius: 50%;
    cursor: pointer;
  }
  .message-banned {
    margin-right: 15px;
    ::v-deep .el-checkbox__inner {
      background-color: transparent;
      border-color: #e4e7ed;
    }
    ::v-deep .el-checkbox__label {
      color: #e4e7ed;
    }
    &.is-checked {
      ::v-deep .el-checkbox__inner {
        border-color: red;
        @include edu-color-primary(background-color);
        @include edu-color-primary(border-color);
      }
      ::v-deep .el-checkbox__label {
        @include edu-color-auxiliary;
      }
    }
  }

  .message-speak {
    background: #666666;
    border-radius: 4px;
    margin-top: 5px;
    padding: 5px 8px;
    .message-input {
      flex: 1;
      ::v-deep .el-textarea__inner {
        color: #fff;
        background-color: transparent;
        border: none;
        padding: 0;
      }
    }
  }
}
</style>
